package top.jlzhou.jobs.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.support.spring.annotation.ResponseJSONP;

import top.jlzhou.R;
import top.jlzhou.jobs.entity.UserInfo;
import top.jlzhou.jobs.service.UserInfoService;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author J.L.Zhou
 * @since 2018-10-22
 */
@Controller
@RequestMapping("/user")
public class UserInfoController {
	
	@Autowired
	private UserInfoService service;

	@RequestMapping("login")
	@ResponseBody
	public R doLogin(String userName,String userPwd,HttpServletRequest request){
		try{
			long token = service.login(userName, userPwd, request.getRemoteAddr());
			return R.ok().put("token", token);
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}


	@RequestMapping("check")
	@ResponseBody
	@ResponseJSONP
	public R doCheck(Long token,HttpServletRequest request){
		try{
			service.check(token, request.getRemoteAddr());
			return R.ok();
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}

	@RequestMapping("logout")
	@ResponseBody
	public R doLogout(Long token,HttpServletRequest request){
		try{
			service.logout(token, request.getRemoteAddr());
			return R.ok();
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}
	
	@RequestMapping("get")
	@ResponseBody
	public R doGet(Long token){
		try{
			
			return R.ok().put("user", service.getByToken(token));
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}
	@RequestMapping("save")
	@ResponseBody
	public R doSave(Long token,UserInfo user){
		try{
			service.save(token, user);
			return R.ok();
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}
	@RequestMapping("pwd")
	@ResponseBody
	public R doPwd(Long token,String pwd1,String pwd2){
		try{
			service.updatePwd(token, pwd1, pwd2);
			return R.ok();
		}catch(Exception ex){
			return R.error(ex.getMessage());
		}
	}
}

